angular.module('SmartCanteenApp.penetration_report', ['ngRoute', 'ngTouch'])

	.config(['$routeProvider', function($routeProvider) {
		$routeProvider.when('/penetrationReport', {  // 新服务中的穿透报表
			templateUrl: 'newReports/penetrationReport.html',
			controller: 'penetrationReportCtrl'
		});
	}])
	//餐卡信息列表
	.controller('penetrationReportCtrl', ['$scope', function($scope) {
		$('#datepicker').datepicker({
			autoclose:true
		})
		//画图折柱图
		$scope.drawChart = function(data){
			$scope.searchDateRange = data.date
			var myChart = echarts.init(document.getElementById('chart'));
			var colors = ['#5793f3', '#d14a61', '#675bba'];
			option = {
				color: colors,
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'cross'
					}
				},
				grid: {
					right: '20%'
				},
				toolbox: {
					feature: {
						dataView: {
							show: true,
							readOnly: false
						},
						magicType: {
							show: true,
							type: ['line', 'bar']
						},
						restore: {
							show: true
						},
						saveAsImage: {
							show: true
						}
					},
					showTitle:false  //是否显示功能的title
				},
				legend: {
					data: ['销售额', '毛利', '毛利占比']
				},
				xAxis: [{
					type: 'category',
					axisTick: {
						alignWithLabel: true
					},
					data: data.x_value
				}],
				yAxis: [{
						type: 'value',
						name: '销售额',
						min: 0,
//						interval: 50, //间隔
//						max: 250,
						position: 'left',
						axisLine: {
							lineStyle: {
								color: colors[0]
							}
						},
						splitLine: {
							show: false //不显示横线格
						},
						axisLabel: {
							formatter: '{value}元'
						}
					},
					{
						type: 'value',
						name: '毛利',
						min: 0,
						position: 'right',
						offset: 50,
						axisLine: {
							lineStyle: {
								color: colors[1]
							}
						},
						splitLine: {
							show: false
						},
						axisLabel: {
							formatter: '{value}元'
						}
					},
					{
						type: 'value',
						name: '毛利占比',
//						min: 0,
//						max:100,
						position: 'right',
						axisLine: {
							lineStyle: {
								color: colors[2]
							}
						},
						axisLabel: {
							formatter: '{value}%'
						}
					}
				],
				tooltip:{
					 trigger: 'axis',
						formatter:function(params)
						        {
						           var relVal = params[0].name;
						           
						           for (var i = 0, l = params.length; i < l; i++) {
						                relVal += '<br/>' + params[i].seriesName + ' : ' + params[i].value+'元';
						            }
						           relVal = relVal.substr(0, relVal.length - 1);  
						           return relVal+"%";
						        }
				},
				series: [{
						name: '销售额',
						type: 'bar',
						data: data.sale_number,
						label: {
			                normal: {
			                    show: true,
			                    position: 'inside',
			                    formatter: '{c}',
//			                    rotate: 90,
//				                align: 'center',
//				                verticalAlign:'middle',
//				                distance: 0
			                }
			            },
			            barWidth : 30,
					},
					{
						name: '毛利',
						type: 'bar',
						yAxisIndex: 1,
						data: data.profit,
						label: {
			                normal: {
			                    show: true,
			                    position: 'top',
			                    formatter: '{c}',
//			                    rotate: 90,
//				                align: 'center',
//				                verticalAlign:'middle',
//				                distance: 0
			                }
			            },
			            barWidth : 30,
					},
					{
						name: '毛利占比',
						type: 'line',
						yAxisIndex: 2,
						data: data.profit_percent,
						label: {
			                normal: {
			                    show: true,
			                    position: 'top',
			                    formatter: '{c}%'
			                }
			            },
					}
				]
			};
			// 使用刚指定的配置项和数据显示图表。
			myChart.setOption(option);
		}
		//打印
		$scope.outPrint = function(){
			var html = $("#tb_itemsales")
			$scope.printHtml(html[0].innerHTML);
		}
		$scope.searchDate = $scope.getNowFormatDate()
		//同比环比切换
		$scope.toggleChart = function(param,e){
			if($scope.searchDate == ''){
				$scope.showError('请选择日期')
				return
			}
			$scope.chartType = param
			$('.toggle').removeClass('btn-primary')
			$(e.target).addClass('btn-primary')
			//获取数据
			$scope.getChartData()
		}
		//页面载入 请求默认柱状图 relative-0 year_on_year-1
		$scope.chartType = 0
		$scope.getChartData = function(){
			var url = $scope.reportPath+'penetrateService/v1/businessDataHistogram?canteenId='+$scope.canteenId+'&date='+$scope.searchDate+'&type='+$scope.chartType
			$scope.getReport(url,'','get',function(data){
				$scope.drawChart(data.data)
			})
		}
		//今日异常总数
		$scope.exception_sum = 0
		//获取下方数据概况
		//表一  单品销售数据
		$scope.getPreview = function(){
			let url = $scope.reportPath + 'penetrateService/v1/itemSaleNumber?canteenId='+$scope.canteenId+'&date='+$scope.searchDate
			$scope.getReport(url,'','get',function(data){
				$scope.item_sale_preview = data.data
				$scope.exception_sum += $scope.item_sale_preview.exception_number
			})
		}
		//表二 营业数据差异分析
		$scope.getSaleNumberProfit = function(){
			let url = $scope.reportPath + 'penetrateService/v1/saleNumberProfit?canteenId='+$scope.canteenId+'&date='+$scope.searchDate
			$scope.getReport(url,'','get',function(data){
				$scope.sale_number_profit = data.data
				$scope.exception_sum += $scope.sale_number_profit.exception_number
			})
		}
		//表五 剩菜管理
		$scope.getLeftover = function(){
			let url = $scope.reportPath + 'penetrateService/v1/ortNumber?canteenId='+$scope.canteenId+'&date='+$scope.searchDate
			$scope.getReport(url,'','get',function(data){
				$scope.leftover = data.data
				$scope.exception_sum += $scope.leftover.exception_number
			})
		}
		//表六 消费数据统计
		$scope.getConsumptionData = function(){
			let url = $scope.reportPath + 'penetrateService/v1/avgPersonConsumeMoney?canteenId='+$scope.canteenId+'&date='+$scope.searchDate
			$scope.getReport(url,'','get',function(data){
				$scope.consumption = data.data
				$scope.exception_sum += $scope.consumption.exception_number
			})
		}
		//获取所有穿透报表下的字表入口数据
		$scope.getSons = function(){
			$scope.getPreview()			//表一
			$scope.getSaleNumberProfit()//表二
			$scope.getLeftover()		//表五
			$scope.getConsumptionData() //表六
		}
		$scope.$watch('searchDate',function(){
			if($scope.searchDate == ''){
//				$scope.searchDate = $scope.getNowFormatDate()
				$scope.showError('请选择日期')
				return
			}
			$scope.getChartData()
			$scope.getSons()	
		})
		$scope.getCanteenName = function(){
			let url = $scope.reportPath + 'commonLittleService/v1/getOneCanteenName?canteenId='+$scope.canteenId
			$scope.getReport(url,'','get',function(data){
				$scope.canteenName = data.data
			})
//			$scope.canteenName = '一食堂'
		}
		//获取食堂Id
		if($scope.getCanteenId() != '') {
			$scope.canteenId = $scope.getCanteenId();
			$scope.getChartData()
			$scope.getCanteenName()
			$scope.getSons()			
		}
		$scope.$on('to-child', function(event, data) {
			//获取从父控制器传来的当前选中食堂
			$scope.canteenId = data;
			$scope.getChartData()
			$scope.getCanteenName()
			$scope.getSons()	
		});
		
	}])

	